# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=level-zero
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-headers"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-loader")
pkgver=1.21.9
pkgrel=1
pkgdesc="API for accessing low level interfaces in oneAPI platform devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://spec.oneapi.com/versions/latest/elements/l0/source/index.html"
msys2_repository_url="https://github.com/oneapi-src/level-zero"
msys2_references=(
  'archlinux: level-zero'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-opencl-headers"
             )
source=("https://github.com/oneapi-src/level-zero/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-mingw-support.patch
        002-missing-include.patch)
sha256sums=('ba543a01adbcbd241518c3eee80b75414094d1fd3efcde9ff2693196cea4d057'
            '69e90b23931b35570c29da2e2db856f4536862d3dd78eb54ecd8f9b38a63fd06'
            '0e6470cb31ea29259df33d5f29b99cf1a0bf44be766b29e53009246759996b30')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-mingw-support.patch
  patch -p1 -i "${srcdir}"/002-missing-include.patch

  printf '%s\n' ${pkgver##*.} > VERSION_PATCH
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  CXXFLAGS+=" -Wno-error=strict-aliasing" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -Wno-dev \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package_level-zero-headers() {
  pkgdesc+=" (headers)"

  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin
  rm -f "${pkgdir}${MINGW_PREFIX}"/lib/*.dll
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}-headers
}

package_level-zero-loader() {
  pkgdesc+=" (loader)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")

  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{include,lib}
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}-loader
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
